package com.atech.ebayadmin.common.pager;

import java.io.Serializable;
import java.util.Collection;

public class Pager implements Serializable {

    private static final long serialVersionUID = -5319485510025861943L;
    private int               totalRows;
    private int               pageSize         = 12;
    private int               currentPage;
    private int               totalPages;
    private int               startRow;
    private int               formNumber;
    private Collection        pageData;

    public Pager(int pageSize) {
        super();
        this.currentPage = 1;
        this.startRow = 0;
        this.pageSize = pageSize;
    }

    public Pager() {
        this.currentPage = 1;
        this.startRow = 0;
    }

    public Collection getPageData() {
        return pageData;
    }

    public void setPageData(Collection pageData) {
        this.pageData = pageData;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getStartRow() {
        return startRow!=0?startRow:(currentPage-1)*pageSize;
    }

    public void setStartRow(int startRow) {
        this.startRow = startRow;
    }

    public int getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }

    public int getTotalRows() {
        return totalRows;
    }

    public void setTotalRows(int totalRows) {
        this.totalRows = totalRows;
        this.totalPages = totalRows / pageSize;
        int mod = totalRows % pageSize;
        if (mod > 0) {
            this.totalPages++;
        }
        if (this.currentPage > totalPages)
            this.currentPage = totalPages;
        this.startRow = (currentPage - 1) * pageSize;
        if (this.startRow < 0)
            startRow = 0;
        if (this.currentPage == 0 || this.currentPage < 0)
            currentPage = 1;
    }

    public void setTotalRows(Number totalRows) {
        this.setTotalRows(totalRows.intValue());
    }

    public void setTotalRows(Long totalRows) {
        this.setTotalRows(totalRows.intValue());
    }

    public int getFormNumber() {
        return formNumber;
    }

    public void setFormNumber(int formNumber) {
        this.formNumber = formNumber;
    }
}
